<?php
use JavaReact\AlibabaOpen\AlibabaClient;
use JavaReact\AlibabaOpen\entity\AddressCodeGetParams;
use JavaReact\AlibabaOpen\entity\CpsMediaProductInfoParams;
use JavaReact\AlibabaOpen\entity\GetCategoryByIdParams;
use JavaReact\AlibabaOpen\entity\ListCybUserGroupFeedParams;
use JavaReact\AlibabaOpen\entity\ListCybUserGroupParams;
use JavaReact\AlibabaOpen\entity\ListOverPricedOfferParams;
use JavaReact\AlibabaOpen\entity\LogisticsInfosParams;
use JavaReact\AlibabaOpen\entity\LogisticsTraceInfoParams;
use JavaReact\AlibabaOpen\entity\OrderDetailParams;
use JavaReact\AlibabaOpen\entity\ProtocolPayParams;
use JavaReact\AlibabaOpen\entity\QueryOfferDetailActivityParams;
use JavaReact\AlibabaOpen\entity\QueryOrderRefundOperationListParams;
use JavaReact\AlibabaOpen\entity\QueryOrderRefundParams;
use JavaReact\AlibabaOpen\entity\UploadRefundVoucherParams;
use app\common\model\SystemConfig as CONFIG;

class Aliopen
{	
	private $webSite=1688;
	public $imageHost='https://cbu01.alicdn.com/';
	
	// 设置配置参数
	public function init($params=[]){
		$conf=CONFIG::get('api.alibaba');
		$obj=new AlibabaClient($params);
		$obj->setAppkey($conf['product_app_key']);
		$obj->setAppsecret($conf['product_app_secret']);
		$obj->setAccessToken($conf['product_token']);
		return $obj;
	}
	
	// 过滤无用参数，输出核心参数
	public function result($data,$exist,$key=''){
		$key=$key==''?$exist:$key;
		if(isset($data[$exist])){
			return spot_key($data,$key);
		}else{
			return $data;
		}
	}
	
	// 获取订单列表
	public function order_list($pageNo=1,$pageSize=20){
		$params=[
			'page'=>$pageNo,
			'pageSize'=>$pageSize
		];
		$api='com.alibaba.trade:alibaba.trade.getSellerOrderList-1';	
		$res=$this->init($params)->order->setApi($api)->get();
		
		$res=$this->result($res,'success','result');
	
		return $res;
	}
	
	// 获取商品列表
	public function product_list($pageNo=1,$pageSize=20,$status=1){
		if($status==1) $status=['published'];
		$params=[
			'pageNo'=>$pageNo,
			'pageSize'=>$pageSize,
			'statusList'=>$status,
		];
		$api='com.alibaba.product:alibaba.product.list.get-1';	
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'success','result.pageResult');
		return $res;
	}
	
	// 获取某个商品的信息
	public function get_product($id){
		if(!is_array($id)){
			$count=1;
			$params=[
				'productID'=>$id,
			];
			$api='alibaba.product.get';
			$res=$this->init($params)->product->setApi($api)->get();
			$res=$this->result($res,'productInfo');
		}else{
			$count=count($id);
			$params=[
				'productIdList'=>$id,
			];
			$api='com.alibaba.product:alibaba.product.getByIdList-1';
			$res=$this->init($params)->product->setApi($api)->get();
			$res=$this->result($res,'success','productList');
		}
		return $res;
	}
	
	//	发布商品
	public function product_add($params){
		// 基础属性
		$data=[
			'productType'	=>	'wholesale',
			'language'		=>	'CHINESE',
			'pictureAuth'	=>	false,
			'webSite'		=>	$this->webSite,
		];
		$params=array_merge($params,$data);
		$api='com.alibaba.product:alibaba.product.add-1';
		$res=$this->init($params)->product->setApi($api)->post();
		return $res;	
	}
	
	//	修改商品
	public function product_up($id,$data){
		$params=[
			'productID'		=>	$id,
			'productInfo'	=>	$data,
			'webSite'		=>	$this->webSite,
		];
		$api='com.alibaba.product:alibaba.product.edit-1';
		$res=$this->init($params)->product->setApi($api)->post();
		return $res;	
	}
	
	// 获取相册列表
	public function album_list(){
		$params=[
			'webSite'=>$this->webSite,
		];
		$api='com.alibaba.product:alibaba.photobank.album.getList-1';
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'albumInfos');
		return $res;
	}
	
	// 获取某个相册下的图片
	public function get_album($id,$pageNo=1,$pageSize=20){
		$params=[
			'webSite'=>$this->webSite,
			'albumID'=>$id
		];
		$api='com.alibaba.product:alibaba.photobank.photo.getList-1';
		$res=$this->init($params)->product->setApi($api)->get();
		if(isset($res['photoInfos'])){
			$data=[];
			foreach($res['photoInfos'] as $v){
				$v['url']=$this->imageHost.$v['url'];
				$data[]=$v;
			}
			$res=$data;
		}
		return $res;
	}
	
	/***************************************************
	 *					非常用接口
	 ***************************************************/
	
	
	// 根据关键字搜索类目
	public function category_keyword($word){
		$params=[
			'keyword'=>$word
		];
		$api='com.alibaba.product:alibaba.category.searchByKeyword-1';
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'products');
		return $res;
	}
	
	// 根据关键字搜索类目
	public function category_id($id){
		$params=[
			'categoryID'=>$id
		];
		$api='com.alibaba.product:alibaba.category.get-1';
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'categoryInfo','categoryInfo.0');
		return $res;
	}
	
	// 	获取叶子类目属性
	public function category_attr($id){
		$params=[
			'categoryID'=>$id,
			'webSite'=>$this->webSite,
		];
		$api='com.alibaba.product:alibaba.category.attribute.get-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	// 	获取叶子类目属性层级级联信息
	public function category_attr_level($id,$value=[]){
		$params=[
			'catId'=>$id,
			'pathValues'=>$value,
		];
		$api='cn.alibaba.open:category.level.attr.get-1';
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'success','categoryFeatures');
		return $res;
	}
	
	//	获取商家的授权品牌信息
	public function brand($id){
		if(!is_array($id)) $id=[$id];
		$params=[
			'leafCatIds'=>$id,			//商品叶子类目id列表
		];
		$api='com.alibaba.member:alibaba.member.brand.query-1';
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'success','result');
		return $res;
	}
	
	//	创建相册
	public function album_add($name,$des='',$type=0,$pass=''){
		$params=[
			'name'=>$name,
			'authority'=>$type,
			'webSite'=>$this->webSite,
		];
		if($des!='') $params['description']=$des;
		if($type==2) $params['password']=$pass;
		
		$api='com.alibaba.product:alibaba.photobank.album.add-1';
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'albumID');
		return $res;
	}
	
	//	修改相册
	public function album_update($id,$name,$des='',$type=0,$pass=''){
		$params=[
			'albumInfo'=>[
				'albumID'=>$id,
				'name'=>$name,
				'authority'=>$type,
				'description'=>$des,
				'imageCount'=>500,
			],
			'webSite'=>$this->webSite,
		];
		if($type==2) $params['password']=$pass;
		
		$api='com.alibaba.product:alibaba.photobank.album.modify-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	//	删除相册
	public function album_delete($id){
		$params=[
			'albumID'=>$id,
			'webSite'=>$this->webSite,
		];		
		$api='com.alibaba.product:alibaba.photobank.album.delete-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	//	上传图片到相册
	public function album_upload($id,$name,$img_url='',$des='',$draw=false){
		$params=[
			'albumID'=>$id,
			'name'=>$name,
			'description'=>$des,
			'drawTxt'=>$draw,
			'webSite'=>$this->webSite,
		];
		if(file_exists($img_url)){
			$params['imageBytes']=base64_encode(image_binary_decode($img_url));
			$api='com.alibaba.product:alibaba.photobank.photo.add-1';
			$res=$this->init($params)->product->setApi($api)->post();
			$res=$this->result($res,'success','categoryFeatures');
		}else{
			$res=[
				'errorCode'=>33,
				'errorMsg'=>'该文件不存在！',
			];
		}
		if($res['errorCode']==0){
			$res['image']['url']=$this->imageHost.$res['image']['url'];
		}
		// 上传成功返回
		// [errorCode] => 0
		// [image] => Array
		// 	(
		// 		[id] => 30996401930
		// 		[name] => color.jpg
		// 		[url] => img/ibank/O1CN0186osSq25kowhXUKEg_!!3999917565-0-cib.jpg
		// 	)
		
		return $res;
	}
	
	//	删除相册里面的图片
	public function album_image_delete($id){
		if(!is_array($id)){
			$params=[
				'photoID'=>$id,
				'webSite'=>$this->webSite,
			];		
			$api='com.alibaba.product:alibaba.photobank.photo.delete-1';
		}else{
			$params=[
				'imageIds'=>$id,  //待操作图片ID列表，每次最多支持删除20张图片。
				'webSite'=>$this->webSite,
			];
			$api='com.alibaba.product:alibaba.photobank.photo.deleteBatch-1';
		}
		
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	//	获取运费模板列表
	public function freight_tpl(){
		$params=[
			'webSite'=>$this->webSite,
		];		
		$api='com.alibaba.logistics:alibaba.logistics.freightTemplate.getList-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	//	获取物流模板详情
	public function myfreight_tpl($id=0,$querySubTemplate=false,$queryRate=false){
		$params=[
			'webSite'=>$this->webSite,
		];
		if($id>0){
			//用于单条查询的场景
			$params['templateId']=$id;
			$params['querySubTemplate']=$querySubTemplate;
			$params['queryRate']=$queryRate;
		}
		$api='com.alibaba.logistics:alibaba.logistics.myFreightTemplate.list.get-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	// 创建我的运费模板-未调试
	public function freight_tpl_add($main,$sub,$rate,$cash_sub='',$cash_rate=''){
		$params=[
			'mainTemplate'=>$main,
			'expressSubTemplate'=>$sub,
			'expressSubRateList'=>$rate,
		];
		if($cash_sub!='') $params['cashSubTemplate']=$cash_sub;
		if($cash_rate!='') $params['cashSubTemplate']=$cash_rate;
		$api='com.alibaba.logistics:alibaba.logistics.myFreightTemplate.create-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	// 修改我的运费模板-未调试
	public function freight_tpl_up($main,$sub,$rate,$cash_sub='',$cash_rate=''){
		$params=[
			'mainTemplate'=>$main,
			'expressSubTemplate'=>$sub,
			'expressSubRateList'=>$rate,
		];
		if($cash_sub!='') $params['cashSubTemplate']=$cash_sub;
		if($cash_rate!='') $params['cashSubTemplate']=$cash_rate;
		$api='com.alibaba.logistics:alibaba.logistics.myFreightTemplate.update-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
	//	我的发货地址列表
	public function address_list(){
		$params=[];
		$api='com.alibaba.logistics:alibaba.logistics.mySendGoodsAddress.list.get-1';
		$res=$this->init($params)->product->setApi($api)->get();
		$res=$this->result($res,'result');
		return $res;
	}
	
	//	添加我的发货地址
	public function address_add($province,$city,$area,$town='',$address,$contactor,$mobile='',$phone='',$post=''){
		$params=[
			'province'	=>	$province,
			'city'		=>	$city,
			'area'		=>	$area,
			'town'		=>	$town,
			'address'	=>	$address,
			'contactor'	=>	$contactor,
			'mobile'	=>	$mobile,
			'phone'		=>	$phone,
			'post'		=>	$post,
		];
		$api='com.alibaba.logistics:alibaba.logistics.mySendGoodsAddress.list.get-1';
		$res=$this->init($params)->product->setApi($api)->get();
		return $res;
	}
	
}

?>